/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package android.sql;

import android.app.ContextoAndroid;
import android.app.Mensaje;
import java.util.ArrayList;
import java.util.List;


public abstract class AbstractAndroid implements IAndroid
{
    protected String strInsertar,strDelete,strUpdate,strTable;
    protected int n;
    private ConexionSqlLite conexion = new ConexionSqlLite();
    
    protected abstract void fillPadrote(Object[] row);
    protected abstract void Comandos();

    protected AbstractAndroid()
    {
        Comandos();
    }
        
    public Boolean update()
    {
        return conexion.exec(strUpdate);
    }

    public Boolean delete()
    {
        return conexion.exec(strDelete);
    }

    public Boolean insertar()
    {
        return conexion.exec(strInsertar);
    }
    
    public int insertarId() {
        conexion.exec(strInsertar);
        String query = "SELECT * FROM "+strTable;
        Table table = new ConexionSqlLite().GetQuery(ContextoAndroid.context,query,n);
        int row = table.getRowCount()-1;
        int col = 0;
        int id = Integer.parseInt(table.getValueAt(row, col));
        return id;
    }

    public Table getQuery(String query, int n)
    {
        return new ConexionSqlLite().GetQuery(ContextoAndroid.context, query, n);
    }
    
   public static AbstractAndroid getAbstractAndroid(AbstractAndroid p, Object value, String tabla, String id) 
    {    
        int num = p.n;
        try{
            String query = "SELECT * FROM "+tabla+" where "+id+" = '" + value + "'";
            Object[] row = new ConexionSqlLite().GetQuery(ContextoAndroid.context,query,num).getRow(0);
            p.fillPadrote(row);
            return p;
        }
        catch(Exception e){Mensaje.Mostrar("EX "+num);return null;}
    }

    public static List<AbstractAndroid> getAll(String tabla, String id, Class clase)
    {
        Table data = new ConexionSqlLite().GetQuery(ContextoAndroid.context,"SELECT "+id+" FROM "+tabla,1);
        List<AbstractAndroid> lst = new ArrayList<AbstractAndroid>();
        for(int x=0;x<data.getRowCount();x++)
            lst.add(AbstractAndroid.getAbstractAndroid(getNewAbstractAndroid(clase),data.getRow(x)[0],tabla,id));
        return lst;
    }
    
    public static List<AbstractAndroid> getAllQuery(String tabla, String id, Class clase,String query)
    {
        Table data = new ConexionSqlLite().GetQuery(ContextoAndroid.context,query+tabla,1);
        List<AbstractAndroid> lst = new ArrayList<AbstractAndroid>();
        for(int x=0;x<data.getRowCount();x++)
            lst.add(AbstractAndroid.getAbstractAndroid(getNewAbstractAndroid(clase),data.getRow(x)[0],tabla,id));
        return lst;
    }
    
    public static AbstractAndroid getNewAbstractAndroid(Class clase)
    {
        AbstractAndroid ret = null;
         if(clase == UsuarioPensamiento.class)
            ret = new UsuarioPensamiento();
        return ret;
    }
}